<?php
/**
 * @Author:      lyt
 * @DateTime:    2016-10-30 17:10:58
 * @Description: Description
 */
class ReportDateReocrd extends Eloquent
{
    protected $table = "report_date_record";

    public static function getDate($begin_date,$end_date,$shop_id,$kye){
	    $export_detail=array();
		$date=ReportDateReocrd::where("report_type",$kye);
        if($shop_id!=0){
            $date=$date->where("shop_id",$shop_id);    
        }        
        $date=$date->whereBetween("date",array($begin_date,$end_date))->get();
        foreach ($date as $_d) {
            $temp=unserialize(gzuncompress(base64_decode($_d->data)));                
            foreach ($temp as $_t) {
                $_t['menDianId'] = $_d->shop_id;
                array_push($export_detail,$_t);
            }
        }
       	return $export_detail;
    }


    /*结算后退库的耗材*/
    public static function get_return_storage_data_for_jxc($shop,$data_id,$begin_date,$end_date){
        $storage_item = array();
        $maintenance_storage_item =ReportDateReocrd::getDate($begin_date,$end_date,$shop,'get_return_storage_data_for_jxc');
        foreach ($maintenance_storage_item as $_maintenance_storage_item){            
            $key = $_maintenance_storage_item['item_type'] . '-' . $_maintenance_storage_item['item_id'];
            if(isset($storage_item[$key])){
                $storage_item[$key]['number'] = $storage_item[$key]['number'] + $_maintenance_storage_item['number'];
                $storage_item[$key]['tax_purchase_subtotal'] = $storage_item[$key]['tax_purchase_subtotal'] + $_maintenance_storage_item['tax_purchase_subtotal'];
            }else{
                $storage_item[$key] = $_maintenance_storage_item;
            }
        }
        return $storage_item;
    }


    /*入库数据*/
    public static function get_storage_data_for_jxc($shop,$data_id,$begin_date,$end_date){
        $storage_item = array();
        $maintenance_storage_item =ReportDateReocrd::getDate($begin_date,$end_date,$shop,'get_storage_data_for_jxc');
        foreach ($maintenance_storage_item as $_maintenance_storage_item){            
            $key = $_maintenance_storage_item['item_type'] . '-' . $_maintenance_storage_item['item_id'];
            if(isset($storage_item[$key])){
                $storage_item[$key]['number'] = $storage_item[$key]['number'] + $_maintenance_storage_item['number'];
                $storage_item[$key]['tax_purchase_subtotal'] = $storage_item[$key]['tax_purchase_subtotal'] + $_maintenance_storage_item['tax_purchase_subtotal'];
            }else{
                $storage_item[$key] = $_maintenance_storage_item;
            }
        }
        return $storage_item;
    }

    /*服务订单维修出库*/
    public static function get_service_order_out_data_for_jxc($shop,$data_id,$begin_date,$end_date){
        $order_out_item = array();
        $maintenance_out_item = ReportDateReocrd::getDate($begin_date,$end_date,$shop,'get_service_order_out_data_for_jxc');
        foreach ($maintenance_out_item as $_maintenance_out_item){                    
            $key = $_maintenance_out_item['item_type'] . '-' . $_maintenance_out_item['item_id'];
            if(isset($order_out_item[$key])){
                $order_out_item[$key]['number'] = $order_out_item[$key]['number'] + $_maintenance_out_item['number'];
                $order_out_item[$key]['tax_purchase_subtotal'] = $order_out_item[$key]['tax_purchase_subtotal'] + $_maintenance_out_item['tax_purchase_subtotal'];
            }else{
                $order_out_item[$key] = $_maintenance_out_item;
            }
        }           
        return $order_out_item;
    }

    /*零售订单销售出库*/
    public static function get_sale_order_out_data_for_jxc($shop,$data_id,$begin_date,$end_date){
        $order_out_item = array();       
        $maintenance_out_item = ReportDateReocrd::getDate($begin_date,$end_date,$shop,'get_sale_order_out_data_for_jxc');
        foreach ($maintenance_out_item as $_maintenance_out_item){          
            $key = $_maintenance_out_item['item_type'] . '-' . $_maintenance_out_item['item_id'];
            if(isset($order_out_item[$key])){
                $order_out_item[$key]['number'] = $order_out_item[$key]['number'] + $_maintenance_out_item['number'];
                $order_out_item[$key]['tax_purchase_subtotal'] = $order_out_item[$key]['tax_purchase_subtotal'] + $_maintenance_out_item['tax_purchase_subtotal'];
            }else{
                $order_out_item[$key] = $_maintenance_out_item;
            }
        }            
        return $order_out_item;
    }

    /*供货商退货出库*/
    public static function get_supplier_out_data_for_jxc($shop,$data_id,$begin_date,$end_date){
        $supplier_out_item = array();
        $maintenance_out_item = ReportDateReocrd::getDate($begin_date,$end_date,$shop,'get_supplier_out_data_for_jxc');
        foreach ($maintenance_out_item as $_maintenance_out_item){           
            $key = $_maintenance_out_item['item_type'] . '-' . $_maintenance_out_item['item_id'];
            if(isset($supplier_out_item[$key])){
                $supplier_out_item[$key]['number'] = $supplier_out_item[$key]['number'] + $_maintenance_out_item['number'];
                $supplier_out_item[$key]['tax_purchase_subtotal'] = $supplier_out_item[$key]['tax_purchase_subtotal'] + $_maintenance_out_item['tax_purchase_subtotal'];
            }else{
                $supplier_out_item[$key] = $_maintenance_out_item;
            }
        }
        return $supplier_out_item;
    }

    /*门店间内部调出出库 总计*/
    public static function get_transfer_out_data_for_jxc($shop,$data_id,$begin_date,$end_date){
        $transfer_out_item = array();
        $maintenance_out_item = ReportDateReocrd::getDate($begin_date,$end_date,$shop,'get_transfer_out_data_for_jxc');        
        foreach ($maintenance_out_item as $_maintenance_out_item){            
            $key = $_maintenance_out_item['item_type'] . '-' . $_maintenance_out_item['item_id'];
            if(isset($transfer_out_item[$key])){
                $transfer_out_item[$key]['number'] = $transfer_out_item[$key]['number'] + $_maintenance_out_item['number'];
                $transfer_out_item[$key]['tax_purchase_subtotal'] = $transfer_out_item[$key]['tax_purchase_subtotal'] + $_maintenance_out_item['tax_purchase_subtotal'];
            }else{
                $transfer_out_item[$key] = $_maintenance_out_item;
            }
        }
        return $transfer_out_item;
    }

    /*门店间内部调出出库  中领用部分*/
    public static function get_transfer_out_data_for_jxc_ly($shop,$data_id,$begin_date,$end_date){
        $transfer_out_item = array();
        $maintenance_out_item = ReportDateReocrd::getDate($begin_date,$end_date,$shop,'get_transfer_out_data_for_jxc_ly');        
        foreach ($maintenance_out_item as $_maintenance_out_item){            
            $key = $_maintenance_out_item['item_type'] . '-' . $_maintenance_out_item['item_id'];
            if(isset($transfer_out_item[$key])){
                $transfer_out_item[$key]['number'] = $transfer_out_item[$key]['number'] + $_maintenance_out_item['number'];
                $transfer_out_item[$key]['tax_purchase_subtotal'] = $transfer_out_item[$key]['tax_purchase_subtotal'] + $_maintenance_out_item['tax_purchase_subtotal'];
            }else{
                $transfer_out_item[$key] = $_maintenance_out_item;
            }
        }
        return $transfer_out_item;
    }

    /*门店间内部调出出库 中调出部分*/
    public static function get_transfer_out_data_for_jxc_dc($shop,$data_id,$begin_date,$end_date){
        $transfer_out_item = array();
        $maintenance_out_item = ReportDateReocrd::getDate($begin_date,$end_date,$shop,'get_transfer_out_data_for_jxc_dc');        
        foreach ($maintenance_out_item as $_maintenance_out_item){            
            $key = $_maintenance_out_item['item_type'] . '-' . $_maintenance_out_item['item_id'];
            if(isset($transfer_out_item[$key])){
                $transfer_out_item[$key]['number'] = $transfer_out_item[$key]['number'] + $_maintenance_out_item['number'];
                $transfer_out_item[$key]['tax_purchase_subtotal'] = $transfer_out_item[$key]['tax_purchase_subtotal'] + $_maintenance_out_item['tax_purchase_subtotal'];
            }else{
                $transfer_out_item[$key] = $_maintenance_out_item;
            }
        }
        return $transfer_out_item;
    }

    /*门店间内部调出出库 中销售部分*/
    public static function get_transfer_out_data_for_jxc_xs($shop,$data_id,$begin_date,$end_date){
        $transfer_out_item = array();
        $maintenance_out_item = ReportDateReocrd::getDate($begin_date,$end_date,$shop,'get_transfer_out_data_for_jxc_xs');        
        foreach ($maintenance_out_item as $_maintenance_out_item){            
            $key = $_maintenance_out_item['item_type'] . '-' . $_maintenance_out_item['item_id'];
            if(isset($transfer_out_item[$key])){
                $transfer_out_item[$key]['number'] = $transfer_out_item[$key]['number'] + $_maintenance_out_item['number'];
                $transfer_out_item[$key]['tax_purchase_subtotal'] = $transfer_out_item[$key]['tax_purchase_subtotal'] + $_maintenance_out_item['tax_purchase_subtotal'];
            }else{
                $transfer_out_item[$key] = $_maintenance_out_item;
            }
        }
        return $transfer_out_item;
    }

    /*盘点盘盈调整入库*/
    public static function get_inventory_storage_data_for_jxc($shop,$data_id,$begin_date,$end_date){
        $inventory_storage_item = array();
        $maintenance_inventory_storage = ReportDateReocrd::getDate($begin_date,$end_date,$shop,'get_inventory_storage_data_for_jxc');     
        foreach ($maintenance_inventory_storage as $_maintenance_inventory_storage){            
            $key = $_maintenance_inventory_storage['item_type'] . '-' . $_maintenance_inventory_storage['item_id'];
            if(isset($inventory_storage_item[$key])){
                $inventory_storage_item[$key]['number'] = $inventory_storage_item[$key]['number'] + $_maintenance_inventory_storage['number'];
                $inventory_storage_item[$key]['tax_purchase_subtotal'] = $inventory_storage_item[$key]['tax_purchase_subtotal'] + $_maintenance_inventory_storage['tax_purchase_subtotal'];
            }else{
                $inventory_storage_item[$key] = $_maintenance_inventory_storage;
            }
        }
        return $inventory_storage_item;
    }
    /*盘点盘亏调整出库*/
    public static function get_inventory_out_data_for_jxc($shop,$data_id,$begin_date,$end_date){
        $inventory_out_item = array();
        $maintenance_inventory_storage = ReportDateReocrd::getDate($begin_date,$end_date,$shop,'get_inventory_out_data_for_jxc'); 
        foreach ($maintenance_inventory_storage as $_maintenance_inventory_storage){            
            $key = $_maintenance_inventory_storage['item_type'] . '-' . $_maintenance_inventory_storage['item_id'];
            if(isset($inventory_out_item[$key])){
                $inventory_out_item[$key]['number'] = $inventory_out_item[$key]['number'] + $_maintenance_inventory_storage['number'];
                $inventory_out_item[$key]['tax_purchase_subtotal'] = $inventory_out_item[$key]['tax_purchase_subtotal'] + $_maintenance_inventory_storage['tax_purchase_subtotal'];
            }else{
                $inventory_out_item[$key] = $_maintenance_inventory_storage;
            }
        }
        return $inventory_out_item;
    }

    /**
     * @Author:      lyt
     * @DateTime:    2017-02-14 15:51:09
     * @Description: 用于统计 各种车辆的
     */
    public static function getShopCarDate($date,$shop){
        $by_new=$by_old=$bj_new=$bj_old=0;
        //查询分类
        $temp_filit=DB::select("SELECT sc3.* FROM service_classification sc3 LEFT JOIN service_classification sc2 on sc3.parent_id=sc2.id LEFT JOIN service_classification sc on sc2.parent_id=sc.id where sc3.`status`=1 and sc.id in(13,14,1553)");
        $filit=array();
        foreach ($temp_filit as $_tf) {
            $filit[]=$_tf->id;
        }

        $order = Orders::whereIn('status',array(1018,1017,1217,1317,1417))
            ->whereBetween('pay_time',array($date." 00:00:00",$date." 23:59:59"))
            ->where('shop_id',$shop)
            ->where('place',2)
            ->orderBy('pay_time',"asc")
            ->get();
        foreach ($order as $_order) {
            $procut_cs=DB::select("SELECT order_product.product_type,service.classify sc,product_package.classify pc FROM  order_product LEFT JOIN service ON service.id = order_product.product_id LEFT JOIN product_package ON product_package.id = order_product.product_id WHERE   order_product.order_id =? AND order_product. STATUS = 1 AND order_product.product_type IN ('base','non_standard')",array($_order->order_id));
            $has_banji=0;
            foreach ($procut_cs as $_pc) {
                $classify=$_pc->product_type=="base"?$_pc->pc:$_pc->sc;
                if(in_array($classify, $filit)){//如果该订存在钣金喷漆项目
                    $has_banji=1;
                    break;
                }
            }
            //查看该订单 是不是一个新订单
            $o=Orders::where("created_at","<",$_order->created_at)->where("car_no",$_order->car_no)->first();
            $k=KingCar::where("car_no",$_order->car_no)->first();
            if($o||$k){//如果存在说明是老订单 
                if($has_banji){//说明是钣金
                    $bj_old++;
                }else{
                    $by_old++;
                }
            }else{//说明是新订单
                if($has_banji){//说明是钣金
                    $bj_new++;
                }else{
                    $by_new++;
                }
            }
        }
        $wash_os = CarWashOrder::where('status',1417)
            ->whereBetween('pay_time',array($date." 00:00:00",$date." 23:59:59"))
            ->where('shop_id',$shop)
            ->orderBy('pay_time',"asc")
            ->get();
        foreach ($wash_os as $wo) {
            //查看该订单 是不是一个新订单
            if($wo->car_no){
                $co = CarWashOrder::where("created_at","<",$wo->created_at)->where("car_no",$wo->car_no)->first();
                $o = Orders::where("created_at","<",$wo->created_at)->where("car_no",$wo->car_no)->first();
            }
            $k = KingCar::where("car_no",$wo->car_no)->first();
            if($co||$o||$k){//如果存在说明是老订单
                $by_old++;
            }else{//说明是新订单
                $by_new++;
            }
        }
        return array(
            array("classes_1"=>"保养接车量","classes_2"=>"新客户","zh"=>"新客户","key"=>"bao_yang_new_custome","value"=>$by_new),
            array("classes_1"=>"保养接车量","classes_2"=>"老客户","zh"=>"老客户","key"=>"bao_yang_old_custome","value"=>$by_old),
            array("classes_1"=>"保养接车量","classes_2"=>"总计","zh"=>"总计","key"=>"bao_yang_all_custome","value"=>$by_old+$by_new),
            array("classes_1"=>"钣金接车量","classes_2"=>"新客户","zh"=>"新客户","key"=>"banjin_new_custom","value"=>$bj_new),
            array("classes_1"=>"钣金接车量","classes_2"=>"老客户","zh"=>"老客户","key"=>"banjin_old_custom","value"=>$bj_old),
            array("classes_1"=>"钣金接车量","classes_2"=>"总计","zh"=>"总计","key"=>"banjin_toal_custom","value"=>$bj_old+$bj_new)
        );

    }

    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-06-06T14:59:16+0800
     * @param    [type]                   $date               [description]
     * @param    [type]                   $shop               [description]
     * @return   [type]                                       [description]
     */
    public static function getOldCustome($date,$shop){       
        $old_num=0;
        $order = Orders::whereIn('status',array(1018,1017,1217,1317,1417))
            ->whereBetween('pay_time',array($date." 00:00:00",$date." 23:59:59"))
            ->where('shop_id',$shop)
            ->where('order_type',1)
            ->orderBy('pay_time',"asc")
            ->get();
        foreach ($order as $_order) {            
            //查看该订单 是不是一个新订单
            $o=Orders::whereIn('status',array(1018,1017,1217,1317,1417))->where("pay_time","<",$date." 00:00:00")->where('order_type',1)
            ->where("car_no",$_order->car_no)->first();
            $k=KingCar::where("car_no",$_order->car_no)->first();
            if($o||$k){//如果存在说明是老订单 
                $old_num++;
            }
        }
        return $old_num;
    }

}